﻿using System;
using System.Collections.Generic;

using XStudio.Core.Data;
using XStudio.Core.Log;

using Core.Manage.User;

namespace Core.Manage.User
{
    /// <summary>
    /// 收购商表规则
    /// </summary>
    public partial class GrainBuyerRule : RuleObject<GrainBuyerEntity, GrainBuyerRule>
    {
        #region 新增收购商表规则
        /// <summary>
        /// 新增收购商表规则
        /// </summary>
        /// <param name="accountID">帐号唯一标识</param>
        /// <param name="name">名称</param>
        /// <param name="address">地址</param>
        /// <param name="likeUserName">联系人</param>
        /// <param name="likePhone">联系电话</param>
        /// <param name="content"></param>
        /// <param name="alipayAccount"></param>
        /// <param name="alipayName"></param>
        /// <param name="createDateTime">创建时间</param>
        /// <param name="createUserID">合作社创建者</param>
        /// <returns>是否新增成功</returns>
        public Result Insert
            (
                int accountID,
                string name,
                string address,
                string likeUserName,
                string likePhone,
                string content,
                string alipayAccount,
                string alipayName,
                int createUserID,
                double commissionRules
            )
        {
            //实体信息
            var curEntity = new GrainBuyerEntity
            {
                AccountID = accountID,
                Name = name,
                Address = address,
                LikeUserName = likeUserName,
                LikePhone = likePhone,
                Content = content,
                AlipayAccount = alipayAccount,
                AlipayName = alipayName,
                CreateDateTime = DateTime.Now,
                CreateUserID = createUserID,
                CommissionRules= commissionRules
            };

            //执行操作并返回
            return Insert(curEntity);
        }

        /// <summary>
        /// 新增收购商表规则
        /// </summary>
        /// <param name="curEntity"></param>
        /// <returns>是否新增成功</returns>
        public new Result Insert(GrainBuyerEntity curEntity)
        {
            // 获取唯一标识
            //if(curEntity.UID == 0)
            //{
            //curEntity.UID = _Db.GetMaxID<GrainBuyerEntity>();
            //}

            // 返回结果对象
            var result = new Result();

            //// 判断唯一性
            //if (GetCount(t => t.Model == curEntity.Model) > 0)
            //{
            //    result.IsOK = false;
            //    result.Description = "已有相同的收购商表，请检查！";
            //    return result;
            //}

            //执行操作并返回
            try
            {
                result.IsOK = _Db.Insert(curEntity);
                result.StateCodeID = result.IsOK ? curEntity.UID : 0;
            }
            catch (Exception ex)
            {
                result.IsOK = false;
                LoggerHelper.WriteError("添加收购商表出现异常！", ex);
            }
            result.Description = string.Format("添加收购商表{0}", (result.IsOK ? "成功。" : "失败！"));
            return result;
        }
        #endregion

        #region 修改收购商表规则
        /// <summary>
        /// 修改收购商表规则
        /// </summary>
        /// <param name="grainBuyerID"></param>
        /// <param name="accountID">帐号唯一标识</param>
        /// <param name="name">名称</param>
        /// <param name="address">地址</param>
        /// <param name="likeUserName">联系人</param>
        /// <param name="likePhone">联系电话</param>
        /// <param name="content"></param>
        /// <param name="alipayAccount"></param>
        /// <param name="alipayName"></param>
        /// <param name="createDateTime">创建时间</param>
        /// <param name="createUserID">合作社创建者</param>
        /// <returns>是否修改成功</returns>
        public Result Update
            (
                int accountID,
                string name,
                string address,
                string likeUserName,
                string likePhone,
                string content,
                string alipayAccount,
                string alipayName,
                double commissionRules
            )
        {
            //实体信息
            var curEntity = new GrainBuyerEntity
            {
                AccountID = accountID,
                Name = name,
                Address = address,
                LikeUserName = likeUserName,
                LikePhone = likePhone,
                Content = content,
                AlipayAccount = alipayAccount,
                AlipayName = alipayName,
                CommissionRules= commissionRules
            };

            //执行操作并返回
            return Update(curEntity, t => t.AccountID == accountID);
        }

        /// <summary>
        /// 修改收购商表规则
        /// </summary>
        /// <param name="curEntity"></param>
        /// <returns>是否新增成功</returns>
        public new Result Update(GrainBuyerEntity curEntity)
        {
            // 返回结果对象
            var result = new Result();

            //// 判断唯一性
            //if (GetCount(t => t.Model == curEntity.Model && t.UID != curEntity.UID) > 0)
            //{
            //    result.IsOK = false;
            //    result.Description = "已有相同的收购商表，请检查！";
            //    return result;
            //}

            //执行操作并返回
            try
            {
                result.IsOK = _Db.Update(curEntity, p => p.UID == curEntity.UID);
            }
            catch (Exception ex)
            {
                result.IsOK = false;
                LoggerHelper.WriteError("修改收购商表出现异常！", ex);
            }
            result.Description = string.Format("修改收购商表{0}", (result.IsOK ? "成功。" : "失败！"));
            return result;
        }
        #endregion

        #region 删除收购商表规则
        /// <summary>
        /// 删除收购商表规则
        /// </summary>
        /// <param name="uid">唯一标识</param>
        /// <returns>是否修改成功</returns>
        public new Result Delete(int uid)
        {
            //执行操作并返回
            var result = new Result();
            result.IsOK = _Db.Delete<GrainBuyerEntity>(p => p.UID == uid);
            result.Description = string.Format("删除收购商表{0}", (result.IsOK ? "成功。" : "失败！"));
            return result;
        }
        #endregion
    }
}
